package com.citrix.gs.util.beacon.job;

import com.citrix.gs.util.RepositoryDeployer;
import com.citrix.gs.util.beacon.BeaconJobType;
import com.citrix.gs.util.beacon.JobConfig;
import com.citrix.gs.util.beacon.Perforce;
import com.citrix.gs.util.common.Config;

public class BeaconSrcJob extends BeaconJob implements Deployable {
    
    public BeaconSrcJob(RepositoryDeployer deployer) {
        super(deployer);
        this.jobType = BeaconJobType.Src;
        this.jobName = this.jobType.name() + "-" + repo.getName();
        this.tempFile = jobType.name().toLowerCase() + "_template.xml";
    }

    @Override
    public void add() {
        
        // Get info for SCM 
        String clientName = JobConfig.getTestClientPrefix() + "-" + jobName ;
        Perforce perforce = (Perforce)repo.getScm();
        String projectPath = perforce.getViewMapString(clientName+"/src");
        
        String userName = null;
        String password = null;
        
        if(perforce.getPort().equals("1111")) {
            userName = Config.me().getProperty("platform.perforce.username");
            password = Config.me().getProperty("platform.perforce.ticket"); // TODO: read the perforce ticket for CI
        } else {
            userName = Config.me().getProperty("citrix.perforce.username");
            password = Config.me().getProperty("citrix.perforce.password");
        }
        
        if(beforeAdd() && editPerforceSCM(userName,password, 
                perforce.getServer(), clientName, projectPath) // Edit SCM(perforce part)
                && editAssignedNode() // Edit assignedNode
                && editAuthToken() // Edit authToken
                //&& editNextJob("Config") // Edit next job
                && editScmTriggerSpec()
                && createJob() // create job
                ) {
            // success
        } else {
            // fail
        }
    }

    @Override
    public void move() {
        if(beforeMove() && updateClient() && updateScmTriggerSpec() && moveJob()) { // TODO: update trigger time
            
        } else {
            
        }
    }

}
